#include "ParserEvent.h"
#include "ComponentsHolder.h"

#include <iostream>

using namespace std;
ParserEvent::ParserEvent(double actionTime) : Event(actionTime, 0)
{
}


ParserEvent::~ParserEvent(void)
{
}


bool ParserEvent::doAction()
{
	static int time=0;

	//ComponentsHolder::getInstance()->getStatisticsHandler()->updateStatistics();

	//Storage* storage = ComponentsHolder::getInstance()->getStorage();
	Parser* parser = ComponentsHolder::getInstance()->getParser();
	
	//storage->printDeviceDistribution();

	time+=SimulationParameters::SimulationTime;
	ComponentsHolder::getInstance()->getStatisticsHandler()->reset(time);
	if (SimulationParameters::flashPolicy == FAST)
	{
		map<string, int>* extents = ComponentsHolder::getInstance()->getStatisticsHandler()->getExtentsHistogram();
		ComponentsHolder::getInstance()->getFlashDevice()->updateExtents(extents);
		ComponentsHolder::getInstance()->getStatisticsHandler()->clearExtentsHistogram();
	}
	if ((SimulationParameters::StaticPlacements == false) && (time%3600==0))
	{
		//ComponentsHolder::getInstance()->getAggregatedStatisticsHandler()->writeToFile();

		//map<int,double> lunsRT = ComponentsHolder::getInstance()->getStatisticsHandler()->getLunStatistics();
		
		//map<string,string>* placements = ComponentsHolder::getInstance()->getPlacementGenerator()->RunGenerator(lunsRT,SimulationParameters::UsePriority, _actionTime, false);
		/*	cout << "*****************************************************************************" << endl;
		cout << "placements size: " << placements->size() << endl;
		cout << "placements size: " << placements->size() << endl;
		cout << "diff placements is: " << (placements->size() - count) << endl;
		cout << "found = " << storage->_found << " miss = " << storage->_miss << endl;
		cout << "*****************************************************************************" << endl;*/
		
		//storage->_found = 0;
		//storage->_miss = 0;
		if (!parser->readIOs())
		{
			//ComponentsHolder::getInstance()->getStatisticsHandler()->updateStatistics();
			ComponentsHolder::getInstance()->getEventsHandler()->setStopSimulation();
			//delete placements;
			return false;
		}
		//storage->clearPlacements();

		//parser->readPlacements(storage->getPlacements(), placements);

		//storage->payForPlacementChanges(time);

		// delete the placements
		//delete placements;
	}
	else if (!parser->readIOs())
	{
		//ComponentsHolder::getInstance()->getStatisticsHandler()->updateStatistics();
		ComponentsHolder::getInstance()->getEventsHandler()->setStopSimulation();
		return false;
	}
	
	ComponentsHolder::getInstance()->getCache()->getCachePercentage();
	
	//if (_actionTime < SimulationParameters::SimulationTime + 1)
	//	ComponentsHolder::getInstance()->getStorage()->resetDevices();
	Event* e = new ParserEvent(_actionTime + SimulationParameters::SimulationTime);
	ComponentsHolder::getInstance()->getEventsHandler()->addEvent(e);

	return true;
}